package com.yequan.hbb.view.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yequan.hbb.R;
import com.yequan.hbb.adapter.LearnAdapter;
import com.yequan.hbb.entries.LearnHP;
import com.yequan.hbb.servers.Servers;
import com.yequan.hbb.utils.ServerUtil;

import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static java.security.AccessController.getContext;

/**
 * A simple {@link Fragment} subclass.
 */
public class LearnHBFragment extends Fragment {


    private LearnAdapter adapter;
    private ListView listView;

    private static LearnHBFragment hbFragment;
    public LearnHBFragment() {
        // Required empty public constructor
    }

    public static LearnHBFragment getInstance() {

        if (hbFragment==null) {
            hbFragment = new LearnHBFragment();

            Bundle args = new Bundle();
            args.putString("empty", "数据加载中....");
            hbFragment.setArguments(args);
        }
        return hbFragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_learn_hb, container, false);
        return inflate;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Bundle arguments = getArguments();
        String empty = arguments.getString("empty");
        listView = ((ListView) view.findViewById(R.id.listView));
        Observable<LearnHP> observable = ServerUtil.createHttpRequest(Servers.class).getLearnMainInfo();

        // listMoveTop = listMoveTop.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())

        adapter = new LearnAdapter(getContext());
        listView.setAdapter(adapter);
        adapter.add(0, 1);
        observable.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<LearnHP>() {
                    @Override
                    public void onCompleted() {
                        Toast.makeText(getContext(), "加载完成...", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(Throwable e) {

                        Log.e("自定义标签", "类名==LearnHBFragment" + "方法名==onError=====:" + e);
                    }

                    @Override
                    public void onNext(LearnHP learnHP) {


                        List<LearnHP.DataBean.CategoryBean> category = learnHP.getData().getCategory();

                          for (int i = 0; i < category.size(); i++) {
                           adapter.add(1+i, category.get(i));


                         }


                    }
                });


    }
}
